package org.compiere.report;

import java.io.File;
import java.io.FileOutputStream;
import java.sql.Timestamp;
import java.util.Properties;
import java.util.logging.Level;

import org.adempiere.exceptions.AdempiereException;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.compiere.model.MAttachmentEntry;
import org.compiere.model.MLocator;
import org.compiere.model.MOrg;
import org.compiere.process.ProcessInfo;
import org.compiere.process.ProcessInfoParameter;
import org.compiere.process.SvrProcess;
import org.compiere.util.Env;
import org.compiere.util.Msg;

import extend.org.compiere.utils.Util;
/**
 * @author A.Kulantayev
 *
 */
public class MM_Form_M17 extends SvrProcess {
	/** Current context */
	private Properties m_ctx;
	private ProcessInfo pi;
	/** */
	private int p_M_Locator_ID;
	/** Period Parameter */
	private Timestamp p_DateAcct_From = null;
	private Timestamp p_DateAcct_To = null;
	/** */
	String file;
	/**
	 * Prepare - e.g., get Parameters.
	 */
	@Override
	protected void prepare() {
		log.info("prepare() method, initial parametrs p_M_Locator_ID, p_DateAcct_From, p_DateAcct_To");
		m_ctx = Env.getCtx();
		pi = getProcessInfo();
		ProcessInfoParameter[] para = getParameter();
		for (int i = 0; i < para.length; i++) {
			String name = para[i].getParameterName();
			if (para[i].getParameter() == null);
			else if (name.equals("M_Locator_ID")) 
				p_M_Locator_ID = para[i].getParameterAsInt();
			else if (name.equals("DateAcct")){
				p_DateAcct_From = (Timestamp)para[i].getParameter();
				p_DateAcct_To = (Timestamp)para[i].getParameter_To();
			}
			else
				log.log(Level.SEVERE, "Unknown Parameter: " + name);
		}
	}

	@Override
	protected String doIt() throws Exception {
		file = Util.localFilePath + "Form_M17_.xls";
		MAttachmentEntry entry = Util.getAttachment(pi, m_ctx, file);
		if (entry == null)
			throw new AdempiereException(Msg.translate(m_ctx,"NotFoundTemplate"));

		HSSFWorkbook wb = new HSSFWorkbook(entry.getInputStream());
		HSSFSheet sheet = wb.getSheetAt(0);
		Row R = null;
		Cell C = null;
		sheet.getRow(8).getCell(0).setCellValue(MOrg.get(m_ctx,Env.getAD_Org_ID(m_ctx)).getName());
		MLocator locator = new MLocator(m_ctx, p_M_Locator_ID, get_TrxName());
		sheet.getRow(13).getCell(14).setCellValue(locator.getValue());
		
	
//		R = sheet.getRow(17);
//		C = R.getCell(29);
//		C.setCellValue(new SimpleDateFormat("dd").format(new Timestamp(System.currentTimeMillis())));
//		C = R.getCell(32);
//		C.setCellValue(new SimpleDateFormat("MMMMM").format(new Timestamp(System.currentTimeMillis())));

		// Write the output to a file
		FileOutputStream fileOut = new FileOutputStream(file);
		wb.write(fileOut);
		fileOut.close();
		// Open the file with the application associated
					Util.launchFile(new File(file));
		return Msg.translate(m_ctx, "Success");
	}
}
